<?php

declare(strict_types=1);

namespace app\api\model;

use app\common\model\TimeModel;
use app\wx\model\SystemAdmin;
use EasyWeChat\Factory;
use NieGe\Redis;

class SupplyOrder extends TimeModel
{
    protected $autoWriteTimestamp = true;

    public function user()
    {
        return $this->belongsTo('SupplyUser', 'user_id', 'id');
    }

    /**
     * 发送管理员消息
     */
    public static function init()
    {
        self::afterInsert(function ($user) {
            $config = [
                'app_id' => 'wxbb5a4404d56eee59',
                'secret' => '55a63f585540c402737b1d5bf6f3b961',
                'token' => 'tan_xianwei',
                'response_type' => 'array',
            ];
            // 使用配置来初始化一个公众号应用实例。
            $app = Factory::officialAccount($config);

            $select = SystemAdmin::select();
            foreach ($select as $k => $v) {
                if ($v['open_id'] == '') continue;
                //向指定用户发送模板消息
                $app->template_message->send([
                    "touser" => $v['open_id'],
                    "template_id" => "0mMrrGSLpSTp58lfSdttvDwP6lnYPnGzpcOQIixepDg",
                    'url' => 'http://www.ydcrms.com:8013/wx/content/info',
                    'data' => [
                        "name" => [
                            "value" => $user['intro'],
                            "color" => "#173177"
                        ],
                    ],
                ]);
            }
        });
    }
}
